package com.cl.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.cl.common.AjaxResult;
import com.cl.handler.BlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {

    @GetMapping("/byR")
    @SentinelResource(value = "byR", blockHandler = "handlerException")
    public AjaxResult byR() {
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("pay", "okOk");
        return ajaxResult;
    }

    @GetMapping("/byA")
    @SentinelResource(value = "byA", blockHandlerClass = BlockHandler.class, blockHandler = "handlerException")
    public AjaxResult byA() {
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("pay", "okOk");
        return ajaxResult;
    }

    public AjaxResult handlerException(BlockException e) {
        AjaxResult ajaxResult = AjaxResult.error();
        ajaxResult.put("e", e.getStackTrace());
        return ajaxResult;
    }
}
